package com.shop2cn.iapi.sqdistributor.testcase;

import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.sqdistributor.parameter.request.GetDistributionAgencyListReq;
import com.shop2cn.iapi.sqdistributor.parameter.response.pojo.DistributorInfoVo;
import com.shop2cn.iapi.sqdistributor.service.GetDistributionAgencyListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.shenqi.SqDistributorService;
import com.ymttest.business.service.shenqi.bean.ShenqiUserSet;
import com.ymttest.database.model.sqdistributor.DistributorRelation;
import com.ymttest.database.sqlwapper.sqdistributor.DistributorRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetDistributionAgencyList {
	
	//java接口
	private static GetDistributionAgencyListReq getDistributionAgencyListReq;
	private static GetDistributionAgencyListCall getDistributionAgencyListCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取分销员机构列表");
	}

	@Before
	public void caseUp() {
		getDistributionAgencyListReq = new GetDistributionAgencyListReq();
		getDistributionAgencyListCall = new GetDistributionAgencyListCall();	
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getDistributionAgencyListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb() throws Exception {
		
		DistributorRelationWrapper distributorRelationWrapper = new DistributorRelationWrapper();

		Long merchantId = SqDistributorService
				.getMasterShopId(getDistributionAgencyListReq.getMerchantId());

		if (merchantId == 0L)
			merchantId = getDistributionAgencyListReq.getMerchantId();
		
		List<DistributorRelation> actualResultList = distributorRelationWrapper
				.selectAgencyListByMerchantId(merchantId);
		List<DistributorInfoVo> expectResultList = getDistributionAgencyListCall
				.getDistributionAgencyListResponse()
				.getDistributorInfoVoList();
		
		if (null != getDistributionAgencyListReq.getDistributorId()
				&& getDistributionAgencyListReq.getDistributorId() != 0) {

			Logger.verifyEquals(expectResultList.size(), 1, "验证列表大小");

			DistributorRelation actualResult = actualResultList
					.stream()
					.filter(agency -> agency.getDistributor_id().equals(
							getDistributionAgencyListReq.getDistributorId()))
					.collect(Collectors.toList()).get(0);
			DistributorInfoVo expectResult = expectResultList.get(0);

			Logger.verifyEquals(expectResult.getId(), actualResult.getId(),
					"验证id");
			Logger.verifyEquals(expectResult.getDistributorId(),
					actualResult.getDistributor_id(), "验证distributorId");
			Logger.verifyEquals(expectResult.getDistributorName(),
					actualResult.getDistributor_name(), "验证distributorName");
			Logger.verifyEquals(expectResult.getDistributorPhone(),
					actualResult.getDistributor_phone(), "验证distributorPhone");
			Logger.verifyEquals(expectResult.getDistributorLevelIndex(),
					actualResult.getDistributor_level_index(),
					"验证distributorLevelIndex");
			Logger.verifyEquals(expectResult.getDistributorStatus(),
					actualResult.getDistributor_status(), "验证distributorStatus");
			Logger.verifyEquals(expectResult.getParentDistributorId(),
					actualResult.getParent_distributor_id(),
					"验证parentDistributorId");
			Logger.verifyEquals(expectResult.getMerchantId(),
					actualResult.getMerchant_id(), "验证merchantId");
			DataUtil.verifyDateEquals(expectResult.getAddTime(),
					actualResult.getAdd_time(), "验证addTime");

		} else {

			Logger.verifyEquals(expectResultList.size(),
					actualResultList.size(), "验证列表大小");

			for (int i = 0; i < expectResultList.size(); i++) {

				DistributorRelation actualResult = actualResultList.get(i);
				DistributorInfoVo expectResult = expectResultList.get(i);

				Logger.verifyEquals(expectResult.getId(), actualResult.getId(),
						"验证id");
				Logger.verifyEquals(expectResult.getDistributorId(),
						actualResult.getDistributor_id(), "验证distributorId");
				Logger.verifyEquals(expectResult.getDistributorName(),
						actualResult.getDistributor_name(), "验证distributorName");
				Logger.verifyEquals(expectResult.getDistributorPhone(),
						actualResult.getDistributor_phone(),
						"验证distributorPhone");
				Logger.verifyEquals(expectResult.getDistributorLevelIndex(),
						actualResult.getDistributor_level_index(),
						"验证distributorLevelIndex");
				Logger.verifyEquals(expectResult.getDistributorStatus(),
						actualResult.getDistributor_status(),
						"验证distributorStatus");
				Logger.verifyEquals(expectResult.getParentDistributorId(),
						actualResult.getParent_distributor_id(),
						"验证parentDistributorId");
				Logger.verifyEquals(expectResult.getMerchantId(),
						actualResult.getMerchant_id(), "验证merchantId");
				DataUtil.verifyDateEquals(expectResult.getAddTime(),
						actualResult.getAdd_time(), "验证addTime");
			}
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributionAgencyList_001() {
		Logger.start(true, "获取分销机构列表,多条数据");
		
		try {
			
			getDistributionAgencyListReq.setMerchantId(500865146L);
			getDistributionAgencyListReq.setPageIndex(1);
			getDistributionAgencyListReq.setPageSize(10);
				
			getDistributionAgencyListCall.setData(getDistributionAgencyListReq);
			getDistributionAgencyListCall.callService();
			
			Logger.verifyEquals(true, getDistributionAgencyListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributionAgencyList_002() {
		Logger.start(true, "获取分销机构列表,指定distributorId");
		
		try {
			
			getDistributionAgencyListReq.setMerchantId(500865146L);
			getDistributionAgencyListReq.setDistributorId(827949966L);
			getDistributionAgencyListReq.setPageIndex(1);
			getDistributionAgencyListReq.setPageSize(10);
				
			getDistributionAgencyListCall.setData(getDistributionAgencyListReq);
			getDistributionAgencyListCall.callService();
			
			Logger.verifyEquals(true, getDistributionAgencyListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetDistributionAgencyList_003() {
		Logger.start(true, "子店铺获取分销机构列表,多条数据");
		
		try {
			
			getDistributionAgencyListReq.setMerchantId(ShenqiUserSet.chainStoreGoodsOwnerId);
			getDistributionAgencyListReq.setPageIndex(1);
			getDistributionAgencyListReq.setPageSize(10);
				
			getDistributionAgencyListCall.setData(getDistributionAgencyListReq);
			getDistributionAgencyListCall.callService();
			
			Logger.verifyEquals(true, getDistributionAgencyListCall.getSuccess(), "验证success");
			
			verifyDb();
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
